﻿using System;
using System.Collections.Generic;
using System.Linq;
using BluePortal.Medias.Core;


namespace BluePortal.Medias.Core
{
    public class BCVideoChapter : AbstractBusinessComponent
    {
        #region IVideoChapterService Members

        public IEnumerable<VideoChapter> FindByVideo(Guid ID)
        {
            var q = from vc in Context.VideoChapters
                    where vc.ID == ID
                    select vc;
            return q.AsEnumerable();
        }

        #endregion

        #region IService<VideoChapter> Members

        public void Create(VideoChapter item)
        {
            AssertNotNull(item, "VideoChapter");
            if (item.CID == Guid.Empty) item.CID = Guid.NewGuid();
            Context.VideoChapters.InsertOnSubmit(item);
            Context.SubmitChanges();
        }

        public void Delete(VideoChapter item)
        {
            AssertNotNull(item, "VideoChapter");
            VideoChapter dbChapter = Context.VideoChapters.Single<VideoChapter>(vc => vc.CID == item.CID);
            Context.VideoChapters.DeleteOnSubmit(dbChapter);
            Context.SubmitChanges();
        }

        public void Update(VideoChapter item)
        {
            AssertNotNull(item, "VideoChapter");
            VideoChapter dbChapter = Context.VideoChapters.Single<VideoChapter>(vc => vc.CID == item.CID);
            dbChapter.Image = item.Image;
            dbChapter.Name = item.Name;
            dbChapter.Time = item.Time;
            Context.SubmitChanges();
        }

        public VideoChapter FindByID(Guid id)
        {
            return Context.VideoChapters.Single<VideoChapter>(c => c.CID == id);
        }

        #endregion
    }
}
